<template>
  <div class="search-box">
    <i class="icon-search"></i>
    <input type="text" class="box" v-model="query" :placeholder="placehoder" ref="sinput" />
    <i v-show="query" class="icon-dismiss" @click="clearQuery"></i>
  </div>
</template>

<script type="text/ecmascript-6">
import {debounce} from 'common/js/tool'

export default {
  name: 'search-box',
  data() {
    return {
      query: ''
    }
  },
  props: {
    placehoder: {
      type: String,
      default: '搜索歌曲、歌手'
    }
  },
  methods: {
    doBlur() {
      this.$refs.sinput.blur()
    },
    clearQuery() {
      this.query = ''
    },
    setQuery(query) {
      this.query = query
    }
  },
  created() {
    this.$watch('query', debounce((newVal) => {
      this.$emit('query', newVal)
    }, 300))
  }
}
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
@import "~common/stylus/variable"

.search-box
  width: 100%
  display: flex
  align-items: center
  padding: 0 6px
  height: 40px // 关键属性
  background: $color-highlight-background
  border-radius: 6px
  box-sizing: border-box
  .icon-search
    font-size: 24px
    color: $color-background
  .box
    flex: 1
    margin: 0 5px
    line-height: 20px
    background: $color-highlight-background
    color: $color-text
    font-size: $font-size-medium
    &::placeholder
      color: $color-text-d
  .icon-dismiss
    font-size: 16px
    color: $color-background

</style>
